﻿using Abp.Dependency;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.ComponentModel.DataAnnotations;

namespace Mt.Site.Core.BadWordsFilters
{
    [AttributeUsage(AttributeTargets.Field|AttributeTargets.Property|AttributeTargets.Parameter,AllowMultiple =true)]
    public class BadWordValiateAttribute: ValidationAttribute, ITransientDependency
    {
        public BadWordValiateAttribute()
        {
        }

        protected override ValidationResult IsValid(object value, ValidationContext validationContext)
        {
            if (value == null || !(value is string v))
            {
                return ValidationResult.Success;
            }

            var _badWordFilterManager = validationContext.GetRequiredService<IBadWordFilterManager>();
            if (!string.IsNullOrWhiteSpace(v))
            {
                var str = _badWordFilterManager.CheckBadWords(v);
                if (!string.IsNullOrWhiteSpace(str))
                {
                    return new ValidationResult(str);
                }
            }

            return ValidationResult.Success;
        }
    }
}
